﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CommodityBase : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
    public GameObject m_DescriptionPanel = null;
    public int m_CommodityID = -1;
    protected bool m_IsHover = false;
    float m_HoverTimeCount = 0;


    protected void Start()
    {
    }

    // Update is called once per frame
    virtual protected void Update()
    {
        if (m_IsHover)
        {
            m_HoverTimeCount += Time.deltaTime;
            if (m_HoverTimeCount > 1f)
            {
                showDescritionPanel();
                m_IsHover = false;
            }
        }
        else
        {
            m_HoverTimeCount = 0;
        }
#if UNITY_ANDROID
        if (!UtilsUnity.IsOnUI(Input.mousePosition) && (Input.touchCount > 1  || Input.GetMouseButtonDown(0)))
        {
            hideDescriptionPanel();
            m_IsHover = false;
            m_HoverTimeCount = 0;
        }
#else
        if (!EventSystem.current.IsPointerOverGameObject())
        {
            hideDescriptionPanel();
            m_IsHover = false;
        }
#endif
        if (Input.mouseScrollDelta != new Vector2(0, 0))
        {
            m_HoverTimeCount = 0;
        }
    }
    public virtual void onBuyClicked()
    {

    }
    public void OnPointerEnter(PointerEventData eventData)
    {
#if UNITY_ANDROID
#else
        hideDescriptionPanel();
        m_IsHover = true;
#endif
    }

    public void OnPointerExit(PointerEventData eventData)
    {
#if UNITY_ANDROID
#else
        m_IsHover = false;
        m_HoverTimeCount = 0;
#endif
    }
    public void OnPointerDown(PointerEventData eventData)
    {
#if UNITY_ANDROID
        hideDescriptionPanel();
        m_HoverTimeCount = 0;
        m_IsHover = true;
#endif
    }

    public void OnPointerUp(PointerEventData eventData)
    {
#if UNITY_ANDROID
        m_IsHover = false;
#endif
    }
    protected virtual void showDescritionPanel()
    {
        if (m_DescriptionPanel == null)
            return;
        m_DescriptionPanel.GetComponent<RectTransform>().position = UtilsUnity.GetPositionInScreen(Input.mousePosition, m_DescriptionPanel.GetComponent<RectTransform>().sizeDelta);
        m_DescriptionPanel.SetActive(true);
    }
    protected virtual void hideDescriptionPanel()
    {
        if (m_DescriptionPanel == null)
            return;
        if (m_DescriptionPanel && m_DescriptionPanel.activeSelf)
            m_DescriptionPanel.SetActive(false);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        showDescritionPanel();
    }
}
